package org.enhance.common.model.po;

import java.math.BigDecimal;
import java.util.Objects;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GeoPoint extends AbstractGeo {

	private static final long serialVersionUID = -58146203972544036L;

	public GeoPoint() {
	}

	public GeoPoint(Double longitude, Double latitude) {
		this.longitude = BigDecimal.valueOf(longitude);
		this.latitude = BigDecimal.valueOf(latitude);
	}

	public GeoPoint(BigDecimal longitude, BigDecimal latitude) {
		this.longitude = longitude;
		this.latitude = latitude;
	}

	public GeoPoint(BigDecimal longitude, BigDecimal latitude, BigDecimal altitude) {
		this.longitude = longitude;
		this.latitude = latitude;
		this.altitude = altitude;
	}

	/* 经度 */
	private BigDecimal longitude;
	/* 纬度 */
	private BigDecimal latitude;
	/* 高度 */
	private BigDecimal altitude;

	@Override
	public String generateShape() {
		StringBuilder sb = new StringBuilder("POINT(");
		sb.append(longitude).append(" ").append(latitude);
		if (altitude != null) {
			sb.append(" ").append(altitude);
		}
		sb.append(")");
		return sb.toString();
	}

	@Override
	public String toString() {
		return "GeoPoint{" + "longitude=" + longitude + ", latitude=" + latitude + ", altitude=" + altitude + '}';
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		GeoPoint geoPoint = (GeoPoint) o;
		boolean sameLongitude = longitude.setScale(8, BigDecimal.ROUND_DOWN).equals(geoPoint.getLongitude().setScale(8, BigDecimal.ROUND_DOWN));
		boolean sameLatitude = latitude.setScale(8, BigDecimal.ROUND_DOWN).equals(geoPoint.getLatitude().setScale(8, BigDecimal.ROUND_DOWN));
		return sameLongitude && sameLatitude;
	}

	@Override
	public int hashCode() {
		return Objects.hash(longitude, latitude, altitude);
	}
}
